import { Component } from "cc";
import { SocketManager } from "./SocketManager";
import { protoCmd } from "../../proto/protoCmd";
import ProtoBufManager from "../../proto/ProtoBufManager";

export class AbstractModel extends Component {
    public constructor() {
        super();
        this.init();
        this.registerInterestedProtocols();
    }
    protected init(): void {

    }
    /**注册协议回调 */
    protected registerInterestedProtocols(): void {

    }
    /**
     * 注册协议回调
     * msg 协议号
     *  */
    public reg(msg: any, callback: Function): void {
        var handler: ProtocolHandler = new ProtocolHandler(msg, callback, this);
        SocketManager.ins.registerHandler(msg, handler);
    }
    /**发送 */
    protected send(msgID: any, protocol: any): void {
        SocketManager.ins.sendRequest(msgID, protocol);
    }
    // public getRequest(): protoCmd.Request {
    //     return ProtoBufManager.ins.pbCmd.Request.create();
    // }
    /**
    * 重置数据
    */
    public reset(): void {

    }

    /**
     * 销毁
     */
    public dispose(): void {
    }
}
export class ProtocolHandler {

    public protocolID: any;
    public callback: Function;
    public thisObj: any;

    public constructor(protocolID: any, callback: Function, thisObj: any) {
        this.protocolID = protocolID;
        this.callback = callback;
        this.thisObj = thisObj;
    }

    public apply(protocol: any): void {
        this.callback.call(this.thisObj, protocol);
    }
}